/*
* SMILParser
*
* Version 1.0
*
* Programmers: Jesse Louderback, Noah Pham, Chad Carpenter, and Marc Hindman
*/
package com.SmilDial;

import java.util.ArrayList;

import CloudPackage.Cloud;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.*;

public class SoundMenu extends Activity {
	Bundle SMIL_Bundle;
	Button btnNext;
	Button btnCancel;
	Button btnPlaySound;
	Button btnStopSound;
	Spinner spinnerSound;
	EditText inputSoundStartTime;
	EditText inputSoundDurationTime;
	private MediaPlayer mediaPlayer;
	String SOUNDLOCATIONPATH = "http://dl.dropbox.com/u/40950565/sddProject/audio/";
	String selectedSong = "";
    String SOUNDEXTENSION = ".mp3";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sound_menu);       
        
        spinnerSound = (Spinner)findViewById(R.id.spinnerSound);
        btnPlaySound = (Button)findViewById(R.id.btnPlaySound);
        btnStopSound = (Button)findViewById(R.id.btnStopSound);
        btnStopSound.setEnabled(false);
        btnNext = (Button)findViewById(R.id.btnNext);
        btnCancel = (Button)findViewById(R.id.btnCancel);
        inputSoundStartTime = (EditText)findViewById(R.id.inputStartTime);
        inputSoundDurationTime = (EditText)findViewById(R.id.inputDurationTime);

        Cloud smilDialCloud = SmilDialActivity.getCloudInstance();
        ArrayList<String> cloudAudio = smilDialCloud.getListOfAudio();
        String[] listOfAudio = cloudAudio.toArray(new String[cloudAudio.size()]);
        
        final String SOUNDLOCATIONPATH = "http://dl.dropbox.com/u/40950565/sddProject/audio/";
        final String SOUNDEXTENSION = ".mp3";
                
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listOfAudio);

        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSound.setAdapter(aa);
                
        btnPlaySound.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				if (spinnerSound.getSelectedItem().toString() != "none") {
					selectedSong = spinnerSound.getSelectedItem().toString();
					try {
						PlayAudio(SOUNDLOCATIONPATH + selectedSong + SOUNDEXTENSION);
						btnStopSound.setEnabled(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					killMediaPlayer();
				}
			}
		});
        
        btnStopSound.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				killMediaPlayer();
			}
		});
        
        btnNext.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				killMediaPlayer();
				Intent intent = new Intent(SoundMenu.this, FinalizeMenu.class);
				
				//Gets the bundle from last intent, TextMenu.java
		        SMIL_Bundle = getIntent().getExtras();
		        
		        String selectedSong = "";
				Float soundStartTime = (float) 0.0;
				Float soundDurationTime = (float) 0.0;
				Boolean inputGood = true;
				
				if (spinnerSound.getSelectedItem().toString() != "none") {
					selectedSong = SOUNDLOCATIONPATH + spinnerSound.getSelectedItem().toString() 
								   + SOUNDEXTENSION;
				}
				
				if (inputSoundStartTime.getText().toString().length() >= 1) {
					inputGood = numValidationInput(inputSoundStartTime.getText().toString());
					soundStartTime = new Float(inputSoundStartTime.getText().toString());
				}
				
				if (inputSoundDurationTime.getText().toString().length() >= 1) {
					inputGood = numValidationInput(inputSoundDurationTime.getText().toString());
					soundDurationTime = new Float(inputSoundDurationTime.getText().toString());
				}

				if (inputGood) {
					SMIL_Bundle.putString("soundURL", selectedSong);
					SMIL_Bundle.putFloat("soundStartTime", soundStartTime);
					SMIL_Bundle.putFloat("soundDurationTime", soundDurationTime);				
			        
			        intent.putExtras(SMIL_Bundle);
					startActivity(intent);
				}
			}
		});
        
        btnCancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				killMediaPlayer();
				startActivity(new Intent(SoundMenu.this, SmilDialActivity.class));
			}
		});
    }   
    
    private void PlayAudio(String url) throws Exception {
    	killMediaPlayer();
    	
    	mediaPlayer = new MediaPlayer();
    	mediaPlayer.setDataSource(url);
    	mediaPlayer.prepare();
    	mediaPlayer.start();
    }
    
    protected void onDestory() {
    	super.onDestroy();
    	killMediaPlayer();
    }
    
    private void killMediaPlayer() {
    	if (mediaPlayer != null) {
    		try {
    			mediaPlayer.release();
    			btnStopSound.setEnabled(false);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    private Boolean numValidationInput(String inputValue) {
    	try {
    		int x = Integer.parseInt(inputValue);
    		
    		return true;
    	}
    	catch(Exception e) {
    		Toast toast=Toast.makeText(this, "Duration time must be a number.", 2000); 
    		toast.show(); 
    	}
    	
    	return false;
    }
}
